<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="addRole"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="roleList">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="assignPerm(row.id)"
                    >分配权限</el-button
                  >
                  <el-button
                    size="small"
                    type="primary"
                    @click="editRole(row.id)"
                    >编辑</el-button
                  >
                  <el-button size="small" type="danger" @click="delRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item v-model="companyInfo.mailbox" label="邮箱">
                <el-input disabled style="width: 400px" />
              </el-form-item>
              <el-form-item v-model="companyInfo.remarks" label="备注">
                <el-input
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        :title="roleForm.id ? '编辑' : '新增'"
        :visible="showDialog"
        @close="btnCancel"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="btnCancel">取消</el-button>
            <el-button size="small" type="primary" @click="btnOK"
              >确定</el-button
            >
          </el-col>
        </el-row>
      </el-dialog>
      <el-dialog title="分配权限" :visible="showPerm" @close="btnPermCancel">
        <el-tree
          :data="permList"
          :props="{ label: 'name' }"
          default-expand-all
          show-checkbox
          check-strictly
          node-key="id"
          ref="permTree"
        />
        <template #footer>
          <el-row type="flex" justify="center">
            <el-button size="small" type="primary" @click="btnPermOK"
              >确定</el-button
            >
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-row>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getCompanyInfo,
  getRoleList,
  delRole,
  addRole,
  getRoleDetail,
  updateRole,
  assignPerm,
} from "@/api/setting";
import { getPermList } from "@/api/permission";
import { listToTreeData } from "@/utils";
export default {
  data() {
    return {
      companyInfo: {},
      page: {
        page: 1,
        pagesize: 2,
        total: 0,
      },
      roleList: [],
      showDialog: false,
      roleForm: {
        name: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: "该项不能为空", trigger: "blur" },
          { max: 10, message: "长度不能超过 10 位", trigger: "blur" },
        ],
        description: [
          { required: true, message: "该项不能为空", trigger: "blur" },
          { max: 50, message: "长度不能超过 10 位", trigger: "blur" },
        ],
      },
      showPerm: false,
      permList: [],
      roleId: "",
    };
  },
  created() {
    this.getCompanyInfo();
    // console.log(this.companyInfo);
    this.getRoleList();
    this.getPermList();
  },
  methods: {
    async getCompanyInfo() {
      this.companyInfo = await getCompanyInfo(
        this.$store.state.user.userInfo.companyId
      );
    },
    async getRoleList() {
      const { rows, total } = await getRoleList(this.page);
      this.roleList = rows;
      this.page.total = total;
      // console.log(rows);
    },
    currentChange(newPage) {
      this.page.page = newPage;
      this.getRoleList();
    },
    async delRole(id) {
      //删除功能
      try {
        await this.$confirm("是否确认删除?");
        await delRole(id);
        this.$message.success("删除成功");
        if (this.roleList.length === 1 && this.page.page > 1) {
          this.page.page -= 1;
        }
        this.getRoleList();
      } catch (error) {
        // console.log(error);
      }
    },
    addRole() {
      this.showDialog = true;
    },
    async editRole(id) {
      this.roleForm = await getRoleDetail(id);
      this.showDialog = true;
    },
    async btnOK() {
      await this.$refs.roleForm.validate();
      if (this.roleForm.id) {
        // 编辑功能
        await updateRole(this.roleForm);
      } else {
        // 新增功能
        await addRole(this.roleForm);
      }
      this.$message.success("增加成功");
      this.getRoleList();
      this.showDialog = false;
    },
    btnCancel() {
      //关闭弹窗后清空数据
      this.roleForm = {
        name: "",
        description: "",
      };
      this.$refs.roleForm.resetFields();
      this.showDialog = false;
    },
    //分配权限
    async assignPerm(id) {
      this.roleId = id;
      const { permIds } = await getRoleDetail(id);
      this.showPerm = true;
      this.$nextTick(() => {
        this.$refs.permTree.setCheckedKeys(permIds);
      });
    },
    async btnPermOK() {
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.permTree.getCheckedKeys(),
      });
      this.$message.success("操作成功");
      this.showPerm = false;
    },
    btnPermCancel() {
      this.$refs.permTree.setCheckedKeys([]);
      this.showPerm = false;
    },
    //渲染数据
    async getPermList() {
      this.permList = listToTreeData(await getPermList(), "0");
    },
  },
};
</script>

<style>
</style>